<?php
namespace App\Libs\Base;

/**
 * 进行数组、对象数据访问
 */
class AccessObject implements \ArrayAccess
{
    protected $container;
	public static function instance($container) {
		if (empty($container)) {
			return $container;
		}
		return new self($container);
	}
    public function __construct($container) {
        $this->container = $container;
    }

    public function getObject() {
        return $this->container;
    }

    public function __get($name) {
		if ($name === 'object') {
			return $this->container;
		}
        return $this->offsetGet($name);
    }

    public function __set($name, $value) {
        $this->offsetSet($name, $value);
    }

    public function offsetSet($offset, $value) {
        if (is_null($offset)) {
            $this->container[] = $value;
        } else {
            $this->container[$offset] = $value;
        }
    }

    public function offsetExists($offset) {
        return isset($this->container[$offset]);
    }

    public function offsetUnset($offset) {
        unset($this->container[$offset]);
    }

    public function offsetGet($offset) {
        return isset($this->container[$offset]) ? $this->container[$offset] : NULL;
    }

    public function __toString() {
        return json_encode($this->container);
    }
}
